﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class State : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strUserGuestID = context.Request.QueryString["userID"];
            if (!string.IsNullOrEmpty(strUserGuestID))
            {
                userID = Convert.ToInt32(strUserGuestID);
            }

            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent
            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0, strUserID.IndexOf("?"));
                strUl = StrStateUl(context, Convert.ToInt32(strUserID),true);
            }
            else
            {
                strUl = StrStateUl(context,userID,false);
            }
            
            sbHtml.Replace("$ContentMini_ul$",strUl);
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }
       /// <summary>
       /// ul
       /// </summary>
       /// <param name="context"></param>
       /// <param name="userID">用户ID</param>
       /// <param name="isFriend">是否是查询别人首页的动态</param>
       /// <returns></returns>
        string StrStateUl(HttpContext context,int userID,bool isFriend)
        {
            string strHtml = context.Cache["user_minicontent_ul_state"].ToString();
            string strStateLi = string.Empty;
            if (isFriend)
            {
                strStateLi =FriendStrStateLi(context, userID);
                if(strStateLi.Trim()=="")
                {
                    strStateLi = "暂无动态!";
                }
                strHtml = strHtml.Replace("$StateLi$",strStateLi );
            }
            else
            {
                strStateLi = MyStrStateLi(context, userID);
                if (strStateLi.Trim() == "")
                {
                    strStateLi = "暂无动态!";
                }
                strHtml = strHtml.Replace("$StateLi$",strStateLi);
            }
            
            return strHtml;
        }
        //好友的N个li
        string FriendStrStateLi(HttpContext context,int userID)
        {
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = context.Cache["user_minicontent_ul_state_li"].ToString();
            //Jy123.BLL.Public.State.StateClass stateClass = new Jy123.BLL.Public.State.StateClass();
            List<Jy123.BLL.Public.State.StateClass> listStateRecordClass = new Jy123.BLL.Public.State().GetState(0, 30, userID,true,false, 0, 0);
            for (int i = 0; i < listStateRecordClass.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$StateDetail$", listStateRecordClass[i].HTML);
                str = str.Replace("$ExpTime$", listStateRecordClass[i].STRTIME);
                sbHtml.Append(str);
            }
            //if (!(listStateRecordClass.Count>0))
            //{
            //    sbHtml = "<ul><li>暂无动态</li></ul>";
            //}
            return sbHtml.ToString();
        }
        //我的N个LI
        string MyStrStateLi(HttpContext context, int userID)
        {
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = context.Cache["user_minicontent_ul_state_li"].ToString();
            //Jy123.BLL.Public.State.StateClass stateClass = new Jy123.BLL.Public.State.StateClass();
            List<Jy123.BLL.Public.State.StateClass> listStateRecordClass = new Jy123.BLL.Public.State().GetState(0, 30, userID,false, true, 0, 0);
            for (int i = 0; i < listStateRecordClass.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$StateDetail$", listStateRecordClass[i].HTML);
                str = str.Replace("$ExpTime$", listStateRecordClass[i].STRTIME);
                sbHtml.Append(str);
            }
            return sbHtml.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
